#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "../utils.h"

void replaceBlank(std::string &str) {
  auto length = str.size();
  int blank_count = count(str.begin(), str.end(), ' ');
  int new_length = length + blank_count * 2;
  str.resize(new_length);
  size_t i = length - 1;
  size_t j = new_length - 1;
  while (i != j) {
    if (str[i] == ' ') {
      str.replace(j - 2, 3, "%20");
      j = j - 3;
      // str[j--] = '0';
      // str[j--] = '2';
      // str[j--] = '%';
    } else {
      str[j--] = str[i];
    }
    --i;
  }
}

void mergeVec(std::vector<int> &vec1, const std::vector<int> &vec2) {
  int length1 = vec1.size();
  int length2 = vec2.size();
  int new_length = length1 + length2;
  int idx1 = length1 - 1;
  int idx2 = length2 - 1;
  int new_idx = new_length - 1;

  vec1.resize(new_length);
  while (idx2 >= 0) {
    if (vec1[idx1] > vec2[idx2]) {
      vec1[new_idx--] = vec1[idx1--];
    } else {
      vec1[new_idx--] = vec2[idx2--];
    }
  }
}

int main() {
  std::string input = "We are happy.";
  std::cout << "The input string is " << input << std::endl;
  replaceBlank(input);
  std::cout << "The replaced string is " << input << std::endl;

  std::vector<int> vec1 = {2, 4, 6, 8, 10};
  std::vector<int> vec2 = {1, 3, 5, 7, 9};
  mergeVec(vec1, vec2);
  printVec("The merged vector is", vec1);
}
